package cn.tedu.oa.attendance.base.config;

import cn.tedu.oa.attendance.constants.DingDingConstant;
import cn.tedu.oa.attendance.consumer.DingDingConsumer;
import cn.tedu.oa.attendance.pojo.po.AttendancePO;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiGettokenRequest;
import com.dingtalk.api.response.OapiGettokenResponse;
import com.dingtalk.open.app.api.GenericEventListener;
import com.dingtalk.open.app.api.OpenDingTalkStreamClientBuilder;
import com.dingtalk.open.app.api.message.GenericOpenDingTalkEvent;
import com.dingtalk.open.app.api.security.AuthClientCredential;
import com.dingtalk.open.app.stream.protocol.event.EventAckStatus;
import com.taobao.api.ApiException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import shade.com.alibaba.fastjson2.JSONObject;

@Component
@Slf4j
public class DingDingConfiguration {

    /**
     * 获取钉钉token
     * @return
     */
    public OapiGettokenResponse getAccessToken() {
        log.debug("获取钉钉token");
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");
        OapiGettokenRequest req = new OapiGettokenRequest();
        req.setAppkey(DingDingConstant.APP_KEY);
        req.setAppsecret(DingDingConstant.APP_SECRET);
        req.setHttpMethod("GET");
        OapiGettokenResponse rsp = null;
        try {
            rsp = client.execute(req);
        } catch (ApiException e) {
            log.debug("获取钉钉token失败：{}",rsp.getErrcode());
        }
        log.debug("获取钉钉token成功：{}",rsp.getBody());
        return rsp;
    }

    /**
     * 订阅Stream模式推送
     */

    public void subscribeEvent(){
        try {
            OpenDingTalkStreamClientBuilder
                    .custom()
                    .credential(new AuthClientCredential(DingDingConstant.APP_KEY, DingDingConstant.APP_SECRET))
                    //注册事件监听
                    .registerAllEventListener(new GenericEventListener() {
                        public EventAckStatus onEvent(GenericOpenDingTalkEvent event) {
                            try {
                                //事件唯一Id
                                String eventId = event.getEventId();
                                //事件类型
                                String eventType = event.getEventType();
                                //事件产生时间
                                Long bornTime = event.getEventBornTime();
                                //获取事件体
                                JSONObject bizData = event.getData();
                                //处理事件
                                DingDingConsumer.process(event.getEventType(),bizData);
                                //消费成功
                                return EventAckStatus.SUCCESS;
                            } catch (Exception e) {
                                //消费失败
                                return EventAckStatus.LATER;
                            }
                        }
                    })
                    .build().start();
        } catch (Exception e) {
            log.error("订阅Stream模式推送失败:{}",e);
        }
    }


}
